<template>
  <div class="bigbox">
    <div class="headerbox">
      <div class="header-left">
        <div>+</div>
        <h2>Todo List</h2>
      </div>
      <div class="header-right">
        <button class="all" @click="handleAllSelect">All</button>
        <button class="del" @click="handleAdd">Add</button>
      </div>
    </div>
    <div class="contextbox">
      <div class="context-item" v-for="(item,index) in todoList" :key="item.id">
        <!-- 选中 -->
        <div class="left" @click="handelSelect(index,item.id)">
          <span
            :style="item.isCheck ? 'opacity: 1' : 'opacity:0'"
          >
          </span>
        </div>
        <!-- 输入 -->
        <!-- 加disable有什么用,没看出区别 -->
        <input type="text" class="content-input" v-model="item.text"
        :disable="item.isCheck" 
        :class="item.isCheck ? 'line-through' : ''"
        @blur="handleBlur"
        ref="inputbox"
        >
        <!-- 时间，删除 -->
        <div class="right">
          <p>{{ item.time }}</p>
          <button @click="handleDel(index,item.id)">Del</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import dayjs from "dayjs"
export default {
  name: 'App',
  data() {
    return {
      todoList: []
    }
  },
  created(){
    try{
      this.todoList = JSON.parse(window.localStorage.getItem('ListTodo'))
    }catch(err){
      console.log(err)
    }
  },
  methods: {
    // 添加列表
    handleAdd() {
      this.todoList.unshift({
        id: this.randomID(),
        isCheck: false,
        text: "",
        time: dayjs(new Date).format("YY-MM-DD HH:mm:ss"),
      })

      const inputLength = this.todoList.length -1
      // 获取焦点
      this.$nextTick(()=>{
        this.$refs.inputbox[inputLength].focus()
      })
    },
    // 生成随机ID
    randomID() {
      return Number(Math.random().toString().substr(2,0) + Date.now()).toString(10)
    },
    // 删除
    handleDel(index,id){
      // console.log(index);
      // console.log(id);
      // 这里不写判断好像也不会怎样
      if(this.todoList[index].id==id){
        this.todoList.splice(index,1)
      }
      this.storage();

    },
    // 选中功能
    handelSelect(index,id){
      // console.log(index);
      // console.log(id);
      if(this.todoList[index].id==id){
        this.todoList[index].isCheck = !this.todoList[index].isCheck;
      }
      this.storage();

    },
    handleAllSelect(){
      this.todoList.forEach(item=>{
        item.isCheck = !item.isCheck;
        // item.isCheck=1;
      })
      this.storage();

    },
    // 存储数据
    storage(){
      window.localStorage.setItem('ListTodo',JSON.stringify(this.todoList));
    },
    // 判断是否输入完成
    handleBlur(){
      this.storage();
    }
  }
}
</script>

<style lang="less">
button {
  padding: 10px 15px;
  border: none;
  border-radius: 4px;
  color: #ffffff;
  margin-left: 10px;

}

.bigbox {
  height: 500px;
  width: 600px;
  background-color: darkgray;
  position: absolute;
  left: 50%;
  top: 50%;
  border-radius: 10px;
  transform: translate(-50%, -50%);
  padding: 20px;
  box-sizing: border-box;
  color: white;
  // overflow-y: scroll;

  .headerbox {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: #ccc 2px solid;

    .header-left {
      display: flex;
      align-items: center;

      div {
        width: 50px;
        background: rgb(107, 151, 202);
        height: 50px;
        border-radius: 50%;
        font-size: 30px;
        text-align: center;
        margin-right: 15px;
      }

    }

    .header-right {

      .del {
        background: #46ca81;
      }

      .all {
        background: #d52332;
      }
    }
  }

  .contextbox {
    margin-top: 50px;
    height: 330px;
    overflow-y: scroll;

    .context-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background: rgb(146, 137, 138);
      border-radius: 8px;
      padding: 10px 20px;
      box-sizing: border-box;
      margin-top: 10px;

      .left {
        width: 30px;
        height: 30px;
        border: 1px solid #ccc;
        border-radius: 50%;
        position: relative;

        span {
          display: inline-block;
          width: 20px;
          height: 20px;
          background: #46ca81;
          border-radius: 50%;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
      }

      
      .content-input {
        flex: 1;
        margin: 0px 10px;
        outline: none;
        background: transparent;
        border: none;
        border-bottom: 1px solid #ccc;
        padding: 5px 10px;
        color: white;

      }
      // 好奇怪，这个改变后的css一定要放在原来的后面，应该是优先级顺序问题，因为前面也设置了color
      .line-through{
        color:rgba(255, 255, 255, 0.3);
        text-decoration: line-through rgba(255, 255, 255, 0.8);
      }
    

      .right {
        display: flex;
        align-items: center;

        button {
          background: #d52332;

        }

      }
    }

  }

}
</style>
